package com.itheima.controller;

import com.itheima.Utils.JwtUtils;
import com.itheima.pojo.*;
import com.itheima.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EmpController {
    @Autowired
    public EmpServices empServices;

    @PostMapping("/Elogin")
    public Result Elogin(@RequestBody User user){
        User user1=empServices.Elogin(user);
        User user2=empServices.stateCheck(user);
        if(user2!=null){
            return new Result(0,"Error_State",null);
        }
        System.out.println(user2);
        System.out.println(user1);
        if(user1!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("name",user1.getName());
            claims.put("password",user1.getPassword());
            String jwt= JwtUtils.generateJwt(claims);
            System.out.println(jwt);
            return Result.success(jwt);
        }
        return Result.error();
    }

    @GetMapping("/eShowOrders")
    public Result eShowOrders(String empName){
        List<OrdersList> ordersLists=empServices.eShowOrders(empName);
        return Result.success(ordersLists);
    }

    @GetMapping("/eAcceptOrder")
    public Result eAcceptOrder(Long orderId,String empName){
        empServices.eAcceptOrder(orderId,empName);
        return Result.success();
    }
    @GetMapping("/arrive")
    public Result arrive(Long orderId){
        empServices.arrive(orderId);
        return Result.success();
    }

    @GetMapping("/eShowHistoryOrders")
    public Result eShowOrders(String empName,int currentPage,int pageSize){
        System.out.println("eshowOrders");
        System.out.println(empName);
        PageBean3 pageBean=empServices.eShowOrders(empName,currentPage,pageSize);
        System.out.println(pageBean);
        return Result.success(pageBean);
    }

    @GetMapping("/eChangePassword")
    public Result eChangePassword(String oldPassword,String newPassword,String empName){
        System.out.println("eChangePassword");
        Result result=empServices.eChangePassword(oldPassword,newPassword,empName);
        return result;
    }
}
